package com.example.mongo.controller;

import com.example.mongo.Model.bean.Shelf;
import com.example.mongo.Model.bean.User;
import com.example.mongo.Model.dto.ResponseData;
import com.example.mongo.dao.UserMapper;
import com.example.mongo.service.ChapterService;
import com.example.mongo.service.ShelfService;
import com.example.mongo.service.UserService;
import com.example.mongo.utils.CookieUtils;
import com.example.mongo.utils.RedisUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/shelf")
public class ShelfController {
    @Autowired
    ShelfService shelfService;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    UserService userService;
    @Autowired
    ResponseData responseData;

    @RequestMapping(value = "/getShelfById/{id}",produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    public String getShelfById(HttpServletRequest request) {

        String id = CookieUtils.isLogin(request, redisUtil);   //判断是否登录，如果cookie中午UUID或者登录过期 返回 “false” 登陆成功返回登录用户 id
        if ("false" == id) {
            responseData.error("400","未登录","");
            return responseData.toString();
        }

        List<Shelf> list =  shelfService.getShelfsById(id);
        if (null==list){
            responseData.error("400","书架还空着哪","");
        }else {
            responseData.success("ok", JSONObject.fromObject(list).toString());
        }

        return responseData.toString();
    }

    //    /test/updatePassword?userName=12312&userPasswword=ming
    @RequestMapping(value = "/insertShelfById",produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    public String insertShelfById(@RequestParam int comicId, HttpServletRequest request) {

        String id = CookieUtils.isLogin(request, redisUtil);   //判断是否登录，如果cookie中午UUID或者登录过期 返回 “false” 登陆成功返回登录用户 id

        if ("false" == id) {
            responseData.error("400","未登录","");
            return responseData.toString();
        }
        //User user = userMapper.getUserById(id); //获取 已经登录的用户信息

        if(null!=shelfService.getShelfsByComicIdAndId(id,comicId)){   // 已存在
            responseData.success("添加成功", "");

        }else {
            Shelf shelf = new Shelf();
            shelf.setComicId(comicId);
            shelf.setId(id);
            shelfService.insertShelf(shelf);
            responseData.success("添加成功", "");
        }

        return responseData.toString();
    }

    @RequestMapping(value = "/deleteShelfById",produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    public String deleteShelfById(@RequestParam int comicId, HttpServletRequest request) {

        String id = CookieUtils.isLogin(request, redisUtil);   //判断是否登录，如果cookie中午UUID或者登录过期 返回 “false” 登陆成功返回登录用户 id

        if ("false" == id) {
            responseData.error("400","未登录","");
            return responseData.toString();
        }
        //User user = userMapper.getUserById(id); //获取 已经登录的用户信息
        shelfService.deleteShelf(shelfService.getShelfsByComicIdAndId(id,comicId));
        responseData.success("删除成功", "");
        return responseData.toString();
    }




}
